package com.itheima.demo;

import com.itheima.domain.Items;
import com.itheima.domain.Orders;
import com.itheima.domain.User;
import com.itheima.mapper.OrdersMapper;
import com.itheima.mapper.UserMapper;
import com.itheima.utils.sessionUtils;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;

import java.io.InputStream;
import java.util.List;

public class Demo {
    //查询所有订单及其所属用户信息
    @Test
    public void findOneTOne() throws Exception {
        /*SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(Resources.getResourceAsStream("sql.xml"));
        SqlSession session = factory.openSession();
*/
        SqlSession session = sessionUtils.getSession();
        OrdersMapper mapper = session.getMapper(OrdersMapper.class);
        List<Orders> list = mapper.findAll();
        for (Orders o : list) {
            System.out.println("订单编号:" + o.getNumber());
            System.out.println("用户名称:" + o.getUser().getUsername());
        }
        sessionUtils.closs();
    }

    //查询用户信息及其订单信息
    @Test
    public void oneTMany() throws Exception{
        /*SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(Resources.getResourceAsStream("sql.xml"));
        SqlSession session = factory.openSession();*/
        SqlSession session = sessionUtils.getSession();
        UserMapper mapper = session.getMapper(UserMapper.class);
        List<User> list = mapper.findAll();

        for (User user : list) {
            System.out.println("用户姓名："+user.getUsername());
            List<Orders> list1 = user.getOrdersList();
            for (Orders orders : list1) {
                System.out.println("订单编号:" + orders.getNumber());
            }
        }
        sessionUtils.closs();
    }

    //查询用户及其所购买商品信息
    @Test
    public void manyTmany() throws Exception {
        /*SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(Resources.getResourceAsStream("sql.xml"));
        SqlSession session = factory.openSession();
*/

        SqlSession session = sessionUtils.getSession();
        UserMapper mapper = session.getMapper(UserMapper.class);
        List<User> list = mapper.findAll();

        for (User user : list) {
            System.out.println("用户id：" + user.getId());
            System.out.println("用户姓名：" + user.getUsername());
            List<Orders> list1 = user.getOrdersList();
            for (Orders orders : list1) {
                List<Items> list2 = orders.getItemsList();
                for (Items items : list2) {
                    System.out.println("商品名称:"+items.getName());
                    System.out.println("商品价格:"+items.getPrice());
                }
            }
        }
        sessionUtils.closs();
    }
    //查询所有订单信息，及其用户的信息和所包含商品信息
    @Test
    public void manyTomany() throws Exception{
       /* SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(Resources.getResourceAsStream("sql.xml"));
        SqlSession session = factory.openSession();*/

        SqlSession session = sessionUtils.getSession();
        OrdersMapper mapper = session.getMapper(OrdersMapper.class);
        List<Orders> list = mapper.findAll();
        for (Orders orders : list) {
            System.out.println("订单编号:"+orders.getNumber());
            User user = orders.getUser();
            System.out.println("用户姓名：" + user.getUsername());
            List<Items> list1 = orders.getItemsList();
            for (Items items : list1) {
                System.out.println("商品名称:"+items.getName());
                System.out.println("商品价格:"+items.getPrice());
            }

            System.out.println("----------------");
        }
        sessionUtils.closs();
    }
}
